import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by lanouhn on 17/7/18.
 */
public class ShowImageByUrl {
    public static  void main(String[] args){
        String myUrl = "http://pic.58pic.com/58pic/14/27/45/71r58PICmDM_1024.jpg";
        try {
            URL url = new URL(myUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            int code = connection.getResponseCode();
            InputStream is = connection.getInputStream();
            if (code==200){
                //缓冲输入流
                 BufferedInputStream bis = new BufferedInputStream(is);
                //缓冲输出流
                File fileImage = new File("/Users/lanouhn/Desktop/idea小程序/tupian.jpg");
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileImage));
                //输出到文件固定代码
                byte[] buffer = new byte[1024];
                int len = -1;
                while ((len = bis.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                    bos.flush();
                }

                bis.close();
                bos.close();
            }

            connection.disconnect();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
